﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tools;

namespace SkladLib
{
    /// <summary>
    /// Interaction logic for Row.xaml
    /// </summary>
    public partial class userRow : UserControl
    {
        public delegate void ChangedEventHandler(object sender, string prefix);
        public event ChangedEventHandler Deleted;
        public static int count = 0;
        public string name = "";
        public string prefix = "";
        public int difLastDay = 0;
        public bool isNew = true;
        private List<TextBox> textBoxes = new List<TextBox>();
        public userRow(string prefix)
        {
            isNew = false;
            InitializeComponent();
            this.prefix = prefix;
            LoadData();
            count++;
            InitTextBoxes();
        }

        private void InitTextBoxes()
        {
            textBoxes.Add(txtday1);
            textBoxes.Add(txtday2);
            textBoxes.Add(txtday3);
            textBoxes.Add(txtday4);
            textBoxes.Add(txtday5);
            textBoxes.Add(txtday6);
            textBoxes.Add(txtday7);
            textBoxes.Add(txtDif);
            textBoxes.Add(txtName);
            textBoxes.Add(txtPrefix);
        }

        public userRow()
        {
            InitializeComponent();
            count++;
            InitTextBoxes();
        }

        public bool isValid()
        {
            foreach (TextBox txtBox in textBoxes)
                if (!Tools.TextBox.TextBoxUpravy.Validate(txtBox, txtBox.Name))
                    return false;
            return true;
        }

        private void TextDay_Changed(object sender, TextChangedEventArgs e)
        {
            Tools.TextBox.TextBoxUpravy.smazatText(sender, e);
        }

        public Dictionary<string, string> ToDictionary()
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("day0", txtday1.Text);
            data.Add("day1", txtday2.Text);
            data.Add("day2", txtday3.Text);
            data.Add("day3", txtday4.Text);
            data.Add("day4", txtday5.Text);
            data.Add("day5", txtday6.Text);
            data.Add("day6", txtday7.Text);
            data.Add("prefix", prefix);
            data.Add("name", name);
            data.Add("dayDif", difLastDay.ToString());
            return data;
        }


        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (Deleted != null)
                Deleted(this,prefix);
        }

        private void LoadData()
        {
            txtPrefix.Text = prefix;
            Dictionary<string, string> profil = DataLib.Settings.databases.dbCeny.QueryGetRow("SELECT * FROM Ceny WHERE prefix='" + prefix + "'");
            txtday1.Text = profil["day0"];
            txtday2.Text = profil["day1"];
            txtday3.Text = profil["day2"];
            txtday4.Text = profil["day3"];
            txtday5.Text = profil["day4"];
            txtday6.Text = profil["day5"];
            txtday7.Text = profil["day6"];
            txtDif.Text = profil["dayDif"];
            txtName.Text = profil["name"];
        }

        private void txtName_TextChanged_1(object sender, TextChangedEventArgs e)
        {
           name = txtName.Text;
        }

        private void txtPrefix_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            prefix = txtPrefix.Text;
        }

        private void txtDif_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            Tools.TextBox.TextBoxUpravy.smazatText(sender, e);
            try
            {
                difLastDay = int.Parse(txtDif.Text);
            }
            catch (Exception ex)
            {}
    }

        private void txtLostFocus(object sender, RoutedEventArgs e)
        {
           if(!Tools.TextBox.TextBoxUpravy.Validate(sender,((TextBox)sender).Name))
                ((TextBox)sender).Text = "0";
        }
    }
}
